package com.edgardeng.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edgardeng.education.entity.JsonResponse;
import com.edgardeng.education.entity.Student;
import com.edgardeng.education.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生 前端控制器
 * </p>
 *
 * @author edgardeng
 * @since 2020-04-28
 */
@RestController
@RequestMapping("/api/student")
public class StudentController extends BaseController {

    @Autowired
    IStudentService baseService;

    @GetMapping()
    public IPage<Student> StudentPage(
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            Student entity) {
        Page<Student> page = new Page<>(current, size); // current page and page size
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        if(entity != null){
            // TODO queury condition
        }
        IPage<Student> StudentPage = baseService.page(page, queryWrapper);
        return StudentPage;
    }

    @PostMapping
    public JsonResponse addStudent(@RequestBody Student entity) {
        boolean r = baseService.save(entity);
        return JsonResponse.result(r);
    }

    @GetMapping("/{id}")
    public Student getStudent(@PathVariable Long id) {
        Student r = this.baseService.getById(id);
        return r;
    }

    @DeleteMapping("/{id}")
    public JsonResponse deleteStudent(@PathVariable Long id) {
        boolean r = this.baseService.removeById(id);
        return JsonResponse.result(r);
    }

    @PutMapping()
    public JsonResponse updateStudent(@RequestBody Student entity) {
        boolean r = this.baseService.updateById(entity);
        return JsonResponse.result(r);
    }

}
